
//
//  ZHNewCharacterController.swift
//  999999新浪微博
//
//  Created by 陈华 on 15/12/18.
//  Copyright © 2015年 华锐. All rights reserved.
//

import UIKit

private let reuseIdentifier = "Cell"

class ZHNewCharacterController: UICollectionViewController {

    /// 新特性界面数量
    private let ItemCount = 4
    
    override func viewDidLoad() {
        super.viewDidLoad()
        ///注册
        self.collectionView!.registerClass(ZHCollectionVCell.self, forCellWithReuseIdentifier: reuseIdentifier)
        self.prenwenCollectionView()
        
       
    }
    
   //流水布局对象
    private let layoutObj = UICollectionViewFlowLayout()
    init(){
        super.init(collectionViewLayout: layoutObj)
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    ///准备collectionView相关参数
    func prenwenCollectionView(){
        layoutObj.itemSize = self.view.bounds.size//item的大小
        layoutObj.minimumInteritemSpacing = 0
        layoutObj.minimumLineSpacing = 0
        collectionView?.bounces = false
        collectionView?.pagingEnabled = true
        layoutObj.scrollDirection = UICollectionViewScrollDirection.Horizontal;
    }
    
    // MARK: UICollectionViewDataSource

    override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        
        return ItemCount
    }
    
    override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        //重队列中获取重用cell
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as! ZHCollectionVCell
        indexPath
        cell.pictureView.image = UIImage(named: "new_feature_\(indexPath.item + 1)")
        if indexPath.item != ItemCount - 1 {//解决cell重用
            cell.beginBnt.hidden = true
        }
        
        return cell
    }
    
    //MAKE: - UICollectionView代理方法
    
    override func collectionView(collectionView: UICollectionView, didEndDisplayingCell cell: UICollectionViewCell, forItemAtIndexPath indexPath: NSIndexPath) {
        
        let visibleItem =  collectionView.indexPathsForVisibleItems().last//跟可见数组中Item得到想要位置
        if visibleItem?.item == ItemCount - 1{
            let Item = collectionView.cellForItemAtIndexPath(visibleItem!)//根据这个位置的到对应的Item
            let ItemNew = Item as? ZHCollectionVCell
            ItemNew!.beginBnt.hidden = false

            //形变属性，和UIview配合
            ItemNew?.beginBnt.transform = CGAffineTransformMakeScale(0, 0)
            UIView.animateWithDuration(2, delay: 0, usingSpringWithDamping: 0.7 , initialSpringVelocity: 5, options: UIViewAnimationOptions(rawValue: 0), animations: { () -> Void in
                
                ItemNew?.beginBnt.transform = CGAffineTransformIdentity//清除transform效果，恢复原样
                }, completion: { (_) -> Void in
                    
            })

        }
    }
        
}









